package com.miao.view
{
	import com.miao.common.AVM1MvoieProxy;
	import com.miao.model.ConfigProxy;
	import com.miao.utils.Consts;
	import com.miao.view.interfaces.IPlayer;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	import spark.components.Image;
	
	public class SwfPlayerMediator extends Mediator implements IPlayer
	{
		
		public static const NAME:String = "SwfPlayerMediator";
		
		private var _currentUrl:String;
		private var _currentMC:MovieClip;
		private var _sliderIsChanging:Boolean=false;
		
		public function SwfPlayerMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			
			initView();
		}
		
		private function initView():void{
			player.swf.addEventListener(Event.COMPLETE, onSwfLoaded);
			player.slider.addEventListener(FlexEvent.CHANGE_START, sliderEvt);
			player.slider.addEventListener(FlexEvent.CHANGE_END, sliderEvt);
			player.currentFrameTxt.addEventListener(FlexEvent.ENTER, currentFrameTxtEvt);
			player.sizeCb.addEventListener(Event.CHANGE, sizeCbEvt);
			player.playBtn.addEventListener(MouseEvent.CLICK, controlBtnMouseEvt);
			player.pauseBtn.addEventListener(MouseEvent.CLICK, controlBtnMouseEvt);
			player.stopBtn.addEventListener(MouseEvent.CLICK, controlBtnMouseEvt);
			
			var context:LoaderContext = new LoaderContext();
			context.applicationDomain = ApplicationDomain.currentDomain;
			context.allowCodeImport = true;
			player.swf.loaderContext = context;
			
			player.playBtn.visible = player.playBtn.includeInLayout=true;
			player.pauseBtn.visible = player.pauseBtn.includeInLayout=false;
		}
		
		private function onSwfLoaded(e:Event):void{
			_currentMC = player.swf.content as MovieClip;
			if(!_currentMC) return;
			
			_currentMC.addEventListener(Event.ENTER_FRAME, handleMovieEnterFrame);
			player.slider.maximum = _currentMC.totalFrames;
			player.totalFramesTxt.text = _currentMC.totalFrames+"";
			
			sizeCbEvt(null);
			play();
		}
		
		public function get isShown():Boolean{
			return player.visible;
		}
		
		public function hide():void{
			player.visible=false;
			player.includeInLayout=false;
		}
		
		public function show():void{
			player.visible=true;
			player.includeInLayout=true;
		}
		
		public function load(localUrl:String):void{
			if(_currentMC) stop();
			
			var bytes:ByteArray = new ByteArray();
			var fs:FileStream = new FileStream();
			fs.open(new File(localUrl), FileMode.READ);
			fs.readBytes(bytes, 0, fs.bytesAvailable);
			fs.close();
			
			player.swf.load(AVM1MvoieProxy.convert2Avm2(bytes));
		}
		
		public function set showLoading(val:Boolean):void{
			player.proBar.visible = player.proBar.includeInLayout = val;
			player.swf.visible = player.swf.includeInLayout = !val;
			player.controlGp.enabled = !val;
		}
		
		public function setProgress(loaded:Number, total:Number):void{
			player.proBar.setProgress(loaded, total);
		}
		
		public function unload():void{
			player.swf.unloadAndStop();
			
			if(_currentMC){
				_currentFrame = 0;
				_currentMC.removeEventListener(Event.ENTER_FRAME, handleMovieEnterFrame);
				_currentMC = null;
			}
			player.currentFrameTxt.text="0";
			player.slider.value=0;
		}
		
		public function play():void{
			if(!_currentMC) return;
			
			if(_currentMC.hasEventListener(Event.ENTER_FRAME)){
				_currentFrame = 0;
				_currentMC.removeEventListener(Event.ENTER_FRAME, handleMovieEnterFrame);
			}
			_currentMC.addEventListener(Event.ENTER_FRAME, handleMovieEnterFrame);
			_currentMC.play();
			player.playBtn.visible = player.playBtn.includeInLayout=false;
			player.pauseBtn.visible = player.pauseBtn.includeInLayout=true;
		}
		
		public function stop():void{
			if(!_currentMC) return;
			
			_currentFrame = 0;
			_currentMC.removeEventListener(Event.ENTER_FRAME, handleMovieEnterFrame);
			_currentMC.gotoAndStop(1);
			player.currentFrameTxt.text="0";
			player.slider.value=0;
			
			player.playBtn.visible = player.playBtn.includeInLayout=true;
			player.pauseBtn.visible = player.pauseBtn.includeInLayout=false;
		}
		
		public function pause():void{
			if(!_currentMC) return;
			
			_currentFrame = 0;
			_currentMC.removeEventListener(Event.ENTER_FRAME, handleMovieEnterFrame);
			_currentMC.stop();
			player.playBtn.visible = player.playBtn.includeInLayout=true;
			player.pauseBtn.visible = player.pauseBtn.includeInLayout=false;
		}
		
		public function gotoAndPlay(pos:Number):void{
			if(!_currentMC) return;
			
			if(_currentMC.hasEventListener(Event.ENTER_FRAME)){
				_currentFrame = 0;
				_currentMC.removeEventListener(Event.ENTER_FRAME, handleMovieEnterFrame);
			}
			_currentMC.addEventListener(Event.ENTER_FRAME, handleMovieEnterFrame);
			_currentMC.gotoAndPlay(pos);
		}
		
		public function set currentUrl(url:String):void{
			_currentUrl = url;
		}
		
		public function get currentUrl():String{
			return _currentUrl;
		}
		
		private function sliderEvt(e:FlexEvent):void{
			if(e.type==FlexEvent.CHANGE_START){
				_sliderIsChanging = true;
			}else if(e.type==FlexEvent.CHANGE_END){
				_sliderIsChanging = false;
				gotoAndPlay(player.slider.value);
			}
		}
		
		private function currentFrameTxtEvt(e:FlexEvent):void{
			if(e.type==FlexEvent.ENTER){
				var frame:int = parseInt(player.currentFrameTxt.text);
				if(frame>0) gotoAndPlay(frame);
			}
		}
		
		private function sizeCbEvt(e:Event):void{
			if(player.sizeCb.selected){
				if(player.swf.content){
					player.swf.width = player.swf.content.width;
					player.swf.height = player.swf.content.height;
				}
			}else{
				player.swf.percentWidth = 100;
				player.swf.percentHeight = 100;
			}
		}
		
		private function controlBtnMouseEvt(e:MouseEvent):void{
			var btn:Image = e.currentTarget as Image;
			if(btn==player.playBtn){
				play();
			}else if(btn==player.pauseBtn){
				pause();
			}else if(btn==player.stopBtn){
				stop();
			}
		}
		
		private var _currentFrame:int=0;
		private function handleMovieEnterFrame(e:Event):void{
			if(_currentMC.currentFrame == _currentFrame){
				_currentFrame = 0;
				_currentMC.removeEventListener(Event.ENTER_FRAME, handleMovieEnterFrame);
				var config:ConfigProxy = facade.retrieveProxy(ConfigProxy.NAME) as ConfigProxy;
				var nextXml:XML = config.getNextXml(_currentUrl);
				if(nextXml) sendNotification(Consts.PLAY_URL, nextXml);
			}
			
			_currentFrame = _currentMC.currentFrame;
			if(!_sliderIsChanging) player.slider.value = _currentFrame;
			if(player.focusManager.getFocus()!=player.currentFrameTxt) player.currentFrameTxt.text = _currentFrame+"";
		}
		
		private function get player():SwfPlayer{
			return viewComponent as SwfPlayer;
		}
	}
}